package lexer

import (
	"fmt"
	"hsql/utils"
	"strings"
)

type SelectStatement struct {
	Fields *[]Field
}

func FieldsLexer(tr *utils.TokenReader) (*[]Field, error) {
	fields := make([]Field, 0)
	tokens := make([]string, 0)
	for {
		token, err := tr.ReadToken()
		if err != nil {
			return nil, err
		}
		if strings.ToUpper(token) == "FROM" {
			field, err := ParseFields(tokens)
			if err != nil {
				return nil, err
			}
			fmt.Println(field)
			fields = append(fields, field)
			break
		}
		if token == "," {
			field, err := ParseFields(tokens)
			if err != nil {
				return nil, err
			}
			fmt.Println(field)
			fields = append(fields, field)
			tokens = make([]string, 0)
		} else {
			tokens = append(tokens, token)
		}
	}

	return &fields, nil
}

func SelectLexer(tr *utils.TokenReader) (Statement, error) {
	stat := &SelectStatement{
		Fields: nil,
	}

	fields, err := FieldsLexer(tr)
	if err != nil {
		return nil, err
	}
	stat.Fields = fields

	return stat, nil
}
